<?php
namespace BP\Frm;
require_once("BP/Difference/Handler/WebContralBase.php");
require_once("BP/Tools/DateUtils.php");
class WF_CCBill_Opt extends WebContralBase {
	public static function constructor__ () 
	{
		$me = new self();
		return $me;
	}
	public function RefBill_Done () 
	{
		try 
		{
			$frmID = $this->GetRequestVal("FrmID");
			$workID = $this->GetRequestValInt64("WorkID");
			$rpt = new GERpt($frmID, $workID);
			$pFrmID = $this->GetRequestVal("PFrmID");
			$pWorkID = $this->GetRequestValInt64("PWorkID");
			$rptP = new GERpt($pFrmID, $pWorkID);
			$rpt->Copy($rptP);
			$rpt->setPWorkID($pWorkID);
			$rpt->SetValByKey("PFrmID", $pFrmID);
			$rpt->Update();
			$gbill = GenerBill::constructor__L($workID);
			$gbill->setPFrmID($pFrmID);
			$gbill->setPWorkID($pWorkID);
			$gbill->Update();
			return "执行成功";
		}
		catch (\RuntimeException $ex)
		{
			return ("err@" . $ex->getMessage());
		}
	}
	public function RefBill_Init () 
	{
		$ds = new DataSet();
		$mapattrs = new MapAttrs();
		$mapattrs->Retrieve($MapAttrAttr->FK_MapData, $this->getFrmID(), $MapAttrAttr->Idx);
		$row = NULL;
		$dt = new DataTable("Attrs");
		$dt->Columns->Add("KeyOfEn", String);
		$dt->Columns->Add("Name", String);
		$dt->Columns->Add("Width", Integer);
		$dt->Columns->Add("UIContralType", Integer);
		$dt->Columns->Add("LGType", Integer);
		foreach ($mapattrs->ToJavaList() as $attr) 		{
			$searchVisable = $attr->getatPara()->GetValStrByKey("SearchVisable");
			if ($searchVisable->equals("0"))
			{
				continue;
			}
			if (($attr->getUIVisible() ==  FALSE ))
			{
				continue;
			}
			$row = $dt->NewRow();
			$row->setValue("KeyOfEn", $attr->getKeyOfEn());
			$row->setValue("Name", $attr->getName());
			$row->setValue("Width", $attr->getUIWidthInt());
			$row->setValue("UIContralType", $attr->getUIContralType());
			$row->setValue("LGType", $attr->getLGType());
			$dt->Rows->add($row);
		}
		$ds->Tables->add($dt);
		$md = new MapData($this->getFrmID());
		$rpts = new GEEntitys($this->getFrmID());
		$attrs = $rpts->getNewEntity()->getEnMap()->getAttrs();
		$qo = new QueryObject($rpts);
		$keyWord = $this->GetRequestVal("SearchKey");
		if ((($DataType->IsNullOrEmpty($keyWord) ==  FALSE ) && ($keyWord->length() >= 1)))
		{
			$qo->addLeftBracket();
			if (($SystemConfig->getAppCenterDBVarStr()->equals("@") || $SystemConfig->getAppCenterDBVarStr()->equals("?")))
			{
				$qo->AddWhere("Title", " LIKE ", ( (($SystemConfig->getAppCenterDBType() == $DBType->MySQL)) ? (((" CONCAT('%'," . $SystemConfig->getAppCenterDBVarStr()) . "SKey,'%')")) : (((" '%'+" . $SystemConfig->getAppCenterDBVarStr()) . "SKey+'%'")) ));
			}
			else
			{
				$qo->AddWhere("Title", " LIKE ", ((" '%'||" . $SystemConfig->getAppCenterDBVarStr()) . "SKey||'%'"));
			}
			$qo->addOr();
			if (($SystemConfig->getAppCenterDBVarStr()->equals("@") || $SystemConfig->getAppCenterDBVarStr()->equals("?")))
			{
				$qo->AddWhere("BillNo", " LIKE ", ( (($SystemConfig->getAppCenterDBType() == $DBType->MySQL)) ? ((("CONCAT('%'," . $SystemConfig->getAppCenterDBVarStr()) . "SKey,'%')")) : ((("'%'+" . $SystemConfig->getAppCenterDBVarStr()) . "SKey+'%'")) ));
			}
			else
			{
				$qo->AddWhere("BillNo", " LIKE ", (("'%'||" . $SystemConfig->getAppCenterDBVarStr()) . "SKey||'%'"));
			}
			$qo->getMyParas()->Add("SKey", $keyWord);
			$qo->addRightBracket();
		}
		else
		{
			$qo->AddHD();
		}
		$dtFrom = $this->GetRequestVal("DTFrom");
		$dtTo = $this->GetRequestVal("DTTo");
		if (($DataType->IsNullOrEmpty($dtFrom) ==  FALSE ))
		{
			if (($dtFrom->trim()->length() == 10))
			{
				$dtFrom += " 00:00:00";
			}
			if (($dtFrom->trim()->length() == 16))
			{
				$dtFrom += ":00";
			}
			$dtFrom = ($DateUtils->addDay($DateUtils->parse($dtFrom, "yyyy-MM-dd"), -1) . " 24:00");
			if ((($dtTo->trim()->length() < 11) || ($dtTo->trim()->indexOf(' ') == -1)))
			{
				$dtTo += " 24:00";
			}
			$qo->addAnd();
			$qo->addLeftBracket();
			$qo->setSQL(((" RDT>= '" . $dtFrom) . "'"));
			$qo->addAnd();
			$qo->setSQL((("RDT <= '" . $dtTo) . "'"));
			$qo->addRightBracket();
		}
		$qo->DoQuery("OID", $this->getPageSize(), $this->getPageIdx());
		$mydt = $rpts->ToDataTableField();
		$mydt->TableName = "DT";
		$ds->Tables->add($mydt);
		return $BP->Tools->Json->ToJson($ds);
	}
}
?>
